package com.example.onlinetaxi.ui.activity.Driver.MainPage.OrderFragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.GetOrderCallBack;
import com.example.onlinetaxi.callback.OrderCallBack.ReceivedOrderCallBack;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.dto.OrderDto.OrderDTO;
import com.example.onlinetaxi.dto.OrderDto.ReceivedOrderDto;
import com.example.onlinetaxi.ui.activity.CommentPage.CommentActivity;
import com.example.onlinetaxi.ui.activity.StartToEndPage.StartToEndActivity;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.example.onlinetaxi.view.Adapter.DriverDoing2OrderAdapter;
import com.example.onlinetaxi.view.Dialog.LoadingDialog;
import com.example.onlinetaxi.view.Dialog.MyDialog;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DriverDoing2OrderFragment extends Fragment {

    private View rootView;
    // 无订单提醒
    private TextView empty;
    // 订单列表
    private List<ReceivedOrderDto> orderList = new ArrayList<>();
    // 重新获取订单
    private GetOrderCallBack getOrderCallBack;
    // 列表
    private RecyclerView recyclerView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_driver_doing2_order, container, false);

        initView();

        initEvent();

        return rootView;
    }

    private void initEvent() {
        // 初始化订单列表控件
        initRecyclerView();

        if (orderList.size() != 0) {
            empty.setVisibility(View.INVISIBLE);
        } else {
            empty.setVisibility(View.VISIBLE);
        }
    }

    private void initRecyclerView() {
        // 找到布局文件中的recyclerView
        RecyclerView recyclerView = rootView.findViewById(R.id.driver_doing_2_order_list);
        /*指定RecyclerView布局方式*/
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        /*指定为LinearLayout布局*/
        recyclerView.setLayoutManager(layoutManager);
        /*适配去管理滑动显示*/
        DriverDoing2OrderAdapter adapter = new DriverDoing2OrderAdapter(orderList);

        // 设置点击事件
        adapter.setReceivedOrderCallBack(new ReceivedOrderCallBack() {
            @Override
            public void jump(ReceivedOrderDto order) {
                // 点击订单页面跳转
                // 跳转到前往终点界面
                startActivity(new Intent(getActivity(), StartToEndActivity.class));
            }

            @Override
            public void clickButton(ReceivedOrderDto order) {
                // 到达按钮
                showDialog(order);
            }
        });

        // 设置适配器
        recyclerView.setAdapter(adapter);
    }


    private void showDialog(ReceivedOrderDto orderDto) {
        View.OnClickListener onCancelClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消，不操作
            }
        };

        View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认
                // 发送请求，创建已完成订单
                Map<String, String> map = new HashMap<>();

                map.put("orderId", orderDto.getReceivedOrderId().toString());

                // 发送订单完成请求
                String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "receivedOrder", "completeOrder");
                DataResponse dataResponse = GetDataResponse.getDataResponse(response);

                if (dataResponse.getCode().equals("200")) {
                    // 开启加载框
                    LoadingDialog.showLoading(getActivity());

                    // 成功
                    Toast.makeText(getActivity(), "订单结束！请提醒用户支付订单~", Toast.LENGTH_SHORT).show();

                    // 获取订单
                    map.clear();
                    map.put("orderId", LocalStorage.currentOrder.getReceivedOrderId().toString());

                    String responseStr = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "completedOrder", "getCompletedOrder");

                    DataResponse dataResponse2 = GetDataResponse.getDataResponse(responseStr);

                    // 配置合适订单
                    Gson gson = new Gson();
                    LocalStorage.currentOrder = gson.fromJson(dataResponse2.getData().get("order").toString(), CompletedOrderDto.class);

                    // 转换时间格式
                    LocalStorage.currentOrder = TimeUtil.setRightTimeFormat(LocalStorage.currentOrder);
                    // 设置当前订单
                    LocalStorage.currentOrderId = LocalStorage.currentOrder.getId().toString();

                    LocalStorage.chosenOrder = LocalStorage.currentOrder;

                    // 跳转到评价乘客页面
                    startActivity(new Intent(getActivity(), CommentActivity.class));

                    // 刷新界面
                    refresh();
                }
            }
        };

        MyDialog.drawable = getActivity().getApplicationContext().getDrawable(R.drawable.dialog_confirm_driver);

        // 显示提示框
        MyDialog.showInfoDialog(getActivity(), "您是否已将乘客送到终点？", "取消", onCancelClickListener, "确认", onConfirmClickListener);
    }

    private void refresh() {
        // 重新获取order列表
        getOrderCallBack.refreshOrder();

        // 刷新当前页面
        initEvent();
    }

    private void initView() {
        empty = rootView.findViewById(R.id.driver_doing_2_order_empty);

        if (orderList.size() != 0) {
            empty.setVisibility(View.INVISIBLE);
        }
    }

    public void setOrderList(List<ReceivedOrderDto> orderList) {
        this.orderList = orderList;
    }

    public void setGetOrderCallBack(GetOrderCallBack getOrderCallBack) {
        this.getOrderCallBack = getOrderCallBack;
    }
}